﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using UDS.Components;

namespace UDS.SubModule.CM.Stat
{
    /// <summary>
    /// sellman 的摘要说明。
    /// </summary>
    public partial class SellManPage : Page
    {
        protected DateTime begintime;
        protected DateTime endtime;

        protected override void OnInit(EventArgs e)
        {
            btn_OK.Click += btn_OK_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                begintime = (Request.QueryString["begintime"] == null) ? DateTime.Now : DateTime.Parse(Request.QueryString["begintime"]);
                endtime = (Request.QueryString["endtime"] == null) ? DateTime.Now : DateTime.Parse(Request.QueryString["endtime"]);
                tbx_begintime.Text = begintime.ToShortDateString();
                tbx_endtime.Text = endtime.ToShortDateString();

                if (Request.QueryString["begintime"] != null)
                {
                    BindData();
                }
                ViewState["begintime"] = begintime.ToShortDateString();
                ViewState["endtime"] = endtime.ToShortDateString();
            }
            else
            {
                begintime = DateTime.Parse(ViewState["begintime"].ToString());
                endtime = DateTime.Parse(ViewState["endtime"].ToString());
            }

            base.OnLoad(e);
        }

        private void BindData()
        {
            begintime = DateTime.Parse(tbx_begintime.Text);
            endtime = DateTime.Parse(tbx_endtime.Text);

            var cm = new Components.CM();
            var staff = new Components.Staff();
            var ds = new DataSet();

            SqlDataReader dr_realname = staff.GetAllStaffs();
            DataTable dt_realname = Tools.ConvertDataReaderToDataTable(dr_realname);
            dt_realname.TableName = "Realname";
            ds.Tables.Add(dt_realname);

            SqlDataReader dr_sellman = cm.GetSellman(begintime, endtime);
            DataTable dt_sellman = Tools.ConvertDataReaderToDataTable(dr_sellman);
            dt_sellman.TableName = "Sellman";
            ds.Tables.Add(dt_sellman);

            SqlDataReader dr_contact = cm.GetContactInfo(begintime, endtime);
            DataTable dt_contact = Tools.ConvertDataReaderToDataTable(dr_contact);
            dt_contact.TableName = "Contact";
            ds.Tables.Add(dt_contact);

            SqlDataReader dr_callincontact = cm.GetCallinContactInfo(begintime, endtime);
            DataTable dt_callincontact = Tools.ConvertDataReaderToDataTable(dr_callincontact);
            dt_callincontact.TableName = "CallinContact";
            ds.Tables.Add(dt_callincontact);

            SqlDataReader dr_newclient = cm.GetNewClient(begintime, endtime);
            DataTable dt_newclient = Tools.ConvertDataReaderToDataTable(dr_newclient);
            dt_newclient.TableName = "NewClient";
            ds.Tables.Add(dt_newclient);

            SqlDataReader dr_new3client = cm.GetNew3StarClient(begintime, endtime);
            DataTable dt_new3client = Tools.ConvertDataReaderToDataTable(dr_new3client);
            dt_new3client.TableName = "New3Client";
            ds.Tables.Add(dt_new3client);

            ds.Relations.Add("staffid_realname", ds.Tables["Sellman"].Columns["SellmanID"], ds.Tables["Realname"].Columns["staff_id"], false);
            ds.Relations.Add("sellman_contact", ds.Tables["Sellman"].Columns["SellmanID"], ds.Tables["Contact"].Columns["MarketmanID"], false);
            ds.Relations.Add("sellman_callincontact", ds.Tables["Sellman"].Columns["SellmanID"], ds.Tables["CallinContact"].Columns["MarketmanID"], false);
            ds.Relations.Add("sellman_newclient", ds.Tables["Sellman"].Columns["SellmanID"], ds.Tables["NewClient"].Columns["AddmanID"], false);
            ds.Relations.Add("sellman_new3client", ds.Tables["Sellman"].Columns["SellmanID"], ds.Tables["New3Client"].Columns["AddmanID"], false);

            dgrd_sellman.DataSource = dt_sellman.DefaultView;
            dgrd_sellman.DataBind();
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            BindData();
        }
    }
}